var mysql = require("mysql");
var connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "123456",
  database: "demo",
  multipleStatements: true,
});

connection.connect();

/**
 * 插入数据总数
 */
var number = 1000000;
/**
 * 一次执行的行数
 */
var size = 1000;

var sql =
  "insert into as_file (table_id,data_id,`path`,`name`,`type`,`size`) values ? ";

/**
 * 一切的开始
 */
count();

/**
 * 计算实际执行的次数
 */
function count() {
  let count = number / size;
  for (let i = 0; i <= count; i++) {
    dosave(sql, createValeus());
  }
}

/**
 * 创建数据
 */
function createValeus() {
  let value = [];
  let valueList = [];
  for (let index = 0; index < size; index++) {
    value = [
      index,
      index,
      "/home/data/archser/serverFile/upload/",
      Math.random() + ".pdf",
      "pdf",
      10,
    ];
    valueList.push(value);
  }
  return valueList;
}

/**
 *
 * @param {sql语句} sql
 * @param {*} valueList
 */
async function dosave(sql, valueList) {
  connection.query(sql, [valueList], function (error, results, fields) {
    if (error) {
      throw error;
    } else {
      console.log("完成");
    }
  });
  connection.query(
    "select max(id) from as_file",
    function (error, results, fields) {
      if (error) {
        throw error;
      } else {
        console.log(results);
      }
    }
  );
}
